/**
 * Created by Administrator on 10/18/2016.
 */
import javax.imageio.*;
import java.io.File;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Picture
{
    public  static void cutPicture()    //剪裁图片
    {
        try{
            BufferedImage image = ImageIO.read(new File("data/pear.jpg"));
            int width = image.getWidth();
            int height = image.getHeight();
            int[] imageArray = new int[width*height];
            //最后一个参数表示相邻两行相同列数的像素存储间隔
            image.getRGB(0,0,width,height,imageArray,0,width);
            BufferedImage imageNew = new BufferedImage(width/2,height,BufferedImage.TYPE_INT_RGB);
            //最后一个元素表示在数组中隔多少个元素再把读取的元素放到下一行的同一列
            imageNew.setRGB(0,0,width/2,height,imageArray,0,width);
            File outFile = new File("data/newPear.jpg");
            if(outFile.exists())
                outFile.delete();   //若该文件存在则删除
            ImageIO.write(imageNew,"jpg",outFile);
            for(int i=0;i<width/2;i++)
            {
                for(int k=0;k<height;k++)
                {
                    getARGB(imageNew.getRGB(i,k));
                }
            }
        }
        catch(IOException e1)
        {
            e1.printStackTrace();
        }
    }

    public static void shrinkPicture()  //缩小图片
    {
        try
        {
            BufferedImage image2 = ImageIO.read(new File("data/pear.jpg"));
            int width = image2.getWidth();
            int height = image2.getHeight();
            int newWidth = width/3;    //缩小后图片的宽变为1/3
            int newHeight = height/3;   //缩小后图片的高变为1/3
            int[] srcPixels = new int[width*height];    //像素的来源数组
            int[] destPixels = new int[newWidth*newHeight];    //像素的目标数组
            image2.getRGB(0, 0, width,height,srcPixels,0,width);
            int srcX;
            int srcY;

            for(int y=0; y<newHeight; y++)
            {
                for(int x=0; x<newWidth; x++ )
                {
                    srcX = (x*width)/newWidth;
                    srcY = (y*height)/newHeight;
                    destPixels[x+y*newWidth] = srcPixels[srcX+srcY*width];
                }
            }   //构建像素的目标数组

            BufferedImage imageNew2 = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_RGB);
            imageNew2.setRGB(0,0,newWidth,newHeight,destPixels,0,newWidth);
            File outFile2 = new File("data/shrinkedPear.jpg");
            if(outFile2.exists())
                outFile2.delete();  //如果该文件存在则删除
            ImageIO.write(imageNew2,"jpg",outFile2);
        }
        catch(IOException e2)
        {
            e2.printStackTrace();
        }
    }

    public static void enlargedPicture()     //放大图片
    {
        try
        {
            BufferedImage image3 = ImageIO.read(new File("data/shrinkedpear.jpg"));
            int width = image3.getWidth();
            int height = image3.getHeight();
            int newWidth = width*2;
            int newHeight = height*2;
            int[] srcPixels2 = new int[height*width];
            int[] destPixels2 = new int[newWidth*newHeight];
            image3.getRGB(0,0,width,height,srcPixels2,0,width);
            int x;  //从源图片中某个像素的坐标
            int y;
            for(int j=0; j<newHeight; j++)
            {
                for(int i=0; i<newWidth; i++)
                {
                    x=(int)((i+0.5)*width/newWidth-0.5);
                    y=(int)((j+0.5)*height/newHeight-0.5);  //双线性插值算法赋值
                    destPixels2[i+j*newWidth] = srcPixels2[x+y*width];  //i和j对应destPixels数组坐标
                }
            }

            BufferedImage imageNew3 = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_RGB);
            imageNew3.setRGB(0,0,newWidth,newHeight,destPixels2,0,newWidth);
            File outFile3 = new File("data/enlargedPear.jpg");
            if(outFile3.exists())
                outFile3.delete();  //如果该文件存在则删除
            ImageIO.write(imageNew3,"jpg",outFile3);
        }
        catch(IOException e3)
        {
            e3.printStackTrace();
        }
    }

    public static void getARGB(int pixel)
    {
        int alpha = (pixel>>24)&0xff;   //&0xff表示取低八位
        int red = (pixel>>16)&0xff;
        int green = (pixel>>8)&0xff;
        int blue = (pixel)&0xff;
        System.out.println("argb:"+alpha+","+red+","+green+","+blue);
    }
}
